<?php 
/**
 * 商品控制器
 */
class GoodsController extends AuthController{
    /**
     * 
     */
    private $model;
     
	public function __auto(){
	    $this->model = K('Goods');
	}
	
	
    public function index(){
    	$data = $this->model->all();
		
		$this->assign('data',$data);
        $this->display(); 
    }
	
	/**
	 * 添加商品
	 */
	public function add(){
		if(IS_POST){
 			if(!$gid=$this->model->addData()){
 				$this->error($this->model->error);
 			}
			
			//把添加商品选中的状态，存入到缓存，下次编辑的时候就可以直接调用了
			//S()函数是缓存方法
			S('selected_' . $gid,$_POST['selected']);
			
			$this->success('添加成功',U('index'));
 		}
		
		//所属分类
		$cateData = K('Cate')->all();
		$cateData = Data::tree($cateData,'cname');
		$this->assign('cateData',$cateData);
		
		//所属品牌
		$brandData = K('Brand')->all();
		$this->assign('brandData',$brandData);
        $this->display(); 
    }
	
	
	/**
	 * 编辑商品
	 */
	public function edit(){
		$gid = Q('get.gid',0,'intval');
//		p($_POST);die;
		if(IS_POST){
			if(!$this->model->editDate($gid)){
				$this->error($this->model->error);
			}
			S('selected_' . $gid,$_POST['selected']);
			$this->success('修改成功', U('index'));
		}
		//读取缓存
		$selected = S('selected_' . $gid);
		$this->assign('selected',$selected);
		//获得旧数据（商品表）
		$oldData = $this->model->where("gid={$gid}")->find();
		$this->assign('oldData',$oldData);
		//商品详细表全部信息
		$DetData = K('Detail')->where("goods_gid={$gid}")->find();
		//商品图册路径单独读取

		$DetData['good_pic'] = unserialize($DetData['good_pic']);
		$this->assign('DetData',$DetData);
		
		//所属分类
		$cateData = K('Cate')->all();
		$cateData = Data::tree($cateData,'cname');
		$this->assign('cateData',$cateData);
		
		//所属品牌
		$brandData = K('Brand')->all();
		$this->assign('brandData',$brandData);
        $this->display(); 
    }
	
	/**
	 * 删除商品
	 */
	 public function del(){
	 	$gid = Q('get.gid',0,'intval');
		//删除商品表中的数据
		//首先删除图片
		$gdata = $this->model->where("gid={$gid}")->find();
		if(is_file($gdata['img'])) unlink($gdata['img']);
		//再删除数据库
		$this->model->where("gid={$gid}")->delete();
		
		//删除商品属性表中的数据
		K('Goods_Attr')->where("goods_gid={$gid}")->delete();
		
		//删除商品详细表中的数据
		$dedata = K('Detail')->where("goods_gid={$gid}")->find();
		$dedata = unserialize($dedata['good_pic']);
		foreach ($dedata as $k => $v) {
			//获得类型
			$type = strrchr($v, '.');
			//小图
			$smallPath = str_replace($type, '_small' . $type, $v);
			//中图
			$midPath = str_replace($type, '_mid' . $type, $v);
			//大图
			$bigPath = str_replace($type, '_big' . $type, $v);	
			//上传图
			$thumbPath = str_replace($type, '_thumb' . $type, $v);		
			$dedata[$k] = array(
				'one' => $v,
				'small' => $smallPath,
				'mid' => $midPath,
				'big' => $bigPath,
				'thumb' => $thumbPath
			);
		}
		foreach ($dedata as $v) {
			foreach ($v as $value) {
				if(is_file($value)) unlink($value);
			}
		}
		K('Detail')->where("goods_gid={$gid}")->delete();
		
		//删除成功提示
		$this->success('删除成功',U('index'),1);
	 }
	
	
	/**
	 * 删除列表图片
	 */
	public function delImg(){
	    if(IS_AJAX){
	    	//删除图片
	    	$path = Q('post.path');
	    	//获得类型
			$type = strrchr($path, '.');
			//小图
			$smallPath = str_replace($type, '_small' . $type, $path);
			if(is_file($smallPath)) unlink($smallPath);
			//中图
			$midPath = str_replace($type, '_mid' . $type, $path);
			if(is_file($midPath)) unlink($midPath);
			//大图
			$bigPath = str_replace($type, '_big' . $type, $path);	
			if(is_file($bigPath)) unlink($bigPath);
			//上传图
			$thumbPath = str_replace($type, '_thumb' . $type, $path);	
			if(is_file($thumbPath)) unlink($thumbPath);	
	    				
			if(is_file($path)) unlink($path);
			//删除数据库中对应文章的图片路径
			if($gid =Q('post.gid',0,'intval')){
				$this->model->where("gid={$gid}")->update(array('img'=>''));
			}
	    }
	}

	/**
	 * 删除列表图片
	 */
	public function delPic(){
	    if(IS_AJAX){
	    	//删除图片
	    	$path = Q('post.path');
	    	//获得类型
			$type = strrchr($path, '.');
			//小图
			$smallPath = str_replace($type, '_small' . $type, $path);
			if(is_file($smallPath)) unlink($smallPath);
			//中图
			$midPath = str_replace($type, '_mid' . $type, $path);
			if(is_file($midPath)) unlink($midPath);
			//大图
			$bigPath = str_replace($type, '_big' . $type, $path);	
			if(is_file($bigPath)) unlink($bigPath);
			//上传图
			$thumbPath = str_replace($type, '_thumb' . $type, $path);	
			if(is_file($thumbPath)) unlink($thumbPath);	
	    	//删除本图片			
			if(is_file($path)) unlink($path);
	    }
	}
	
	
	
	/**
	 * 通过cid获得类型属性
	 */
	public function getTypeAttr(){
	    if(!IS_AJAX) return;
		$cid = Q('post.cid',0,'intval');
		$data = M()->join('__category__ c JOIN __attribute__ at ON c.type_tid=at.type_tid')->where("c.cid={$cid}")->all();
//		p($data); p出来以后页面就不能接收了
		//把类型属性的值按照,拆分,再把压入原数组
		foreach ($data as $k => $v) {
			$data[$k]['at_value'] = explode('，', $v['at_value']);
		}
		echo json_encode($data);
		exit;
	}
	
	/**
	 * 列表图上传
	 */
	public function uploadList(){
		$upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
	/*
	 * 商品图册上传
	 */
	public function uploadPic(){
		$upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {//上传成功
    		$type = strrchr($file[0]['path'], '.');
    		//缩略操作
    		$img = new Image();
			
			//缩略小图
			$smallPath = str_replace($type, '_small' . $type, $file[0]['path']);
			$small = $img->thumb($file[0]['path'],$smallPath,58,58,2);
			//缩略中图
			$midPath = str_replace($type, '_mid' . $type, $file[0]['path']);
			$mid = $img->thumb($file[0]['path'],$midPath,400,400,2);
			//缩略大图
			$bigPath = str_replace($type, '_big' . $type, $file[0]['path']);
			$big = $img->thumb($file[0]['path'],$bigPath,800,800,2);
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
	
	
 }
 
 
 
 


 ?>